﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using AutoCAD;

namespace CADAutoPlot
{
    class Program
    {
        private static AcadApplication cadApp;
        private static AcadModelSpace modelSpace;
        private static AcadDocument pDoc;
        private static AcadSelectionSet selectionSet;
        private static string paramsFile;
        private static string outputFile;
        private static string previewFile;
        private static int width = 4000;
        private static int height = 4000;
        private static Preview previewImg; 
        private static Dictionary<string, double> datas = new Dictionary<string, double>();
        private static Dictionary<string,Object> windowDatas = new Dictionary<string, Object>();
        static void fillData(string path)
        {
            int i = 0;
            try
            {
                StreamReader sr = new StreamReader(path);
                string line;
                line = sr.ReadLine();
                while(line != null)
                {
                    ++i;
                    if (!line.Contains('='))
                    {
                        line = sr.ReadLine();
                        continue;
                    }
                    
                    string key = line.Substring(0, line.IndexOf("="));
                    double value = Double.Parse(line.Substring(line.IndexOf("=") + 1, line.Length - line.IndexOf("=") - 1));
                    //Console.WriteLine(key + "," + value.ToString());
                    datas.Add(key, value);
                    line = sr.ReadLine();
  
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("行号：" + i + ":" + ex.Message);
            }
        }

        static void fillWindowData(string path)
        {
            int i = 0;
            try
            {
                StreamReader sr = new StreamReader(path);
                string line;
                line = sr.ReadLine();
                while (line != null)
                {
                    ++i;
                    if (!line.Contains('='))
                    {
                        line = sr.ReadLine();
                        continue;
                    }

                    string key = line.Substring(0, line.IndexOf("="));
                    string value = line.Substring(line.IndexOf("=") + 1, line.Length - line.IndexOf("=") - 1);
                    //Console.WriteLine(key + "," + value.ToString());
                    windowDatas.Add(key, value);
                    line = sr.ReadLine();

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("行号：" + i + ":" + ex.Message);
            }
        }

        public static int Main(string[] args)
        {
            Console.WriteLine("started!");
            if(args.Length < 2)
            {
                Console.WriteLine("params error");
                return -1;
            }
            paramsFile = args[0];
            outputFile = args[1];
            previewFile = args[1];
            //加载数据

            //fillData(paramsFile);
            fillWindowData(paramsFile);

            
            try
            {
                cadApp = new AcadApplication();
                cadApp.Visible = false;
                pDoc = cadApp.ActiveDocument;
                modelSpace = pDoc.ModelSpace;
                previewImg = new Preview(width, height);
                Console.WriteLine("CAD初始化完成...");

                //处理数据
                //DrawWindow drawWindow = new DrawWindow(modelSpace, datas, previewImg);
                //drawWindow.Draw();
                DrawWindow drawWindow = new DrawWindow(modelSpace, windowDatas, previewImg);
                drawWindow.DrawMultiWindows();

                Console.WriteLine("数据处理完成...");
                //输出
                pDoc.SaveAs(outputFile);
                pDoc.Close();
                cadApp.Quit();

                previewFile = previewFile.Substring(0, previewFile.LastIndexOf(".")) + ".jpg";
                previewImg.saveImg(previewFile);
                return 0;
            }
           
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.TargetSite);
                Console.WriteLine(e.StackTrace);
                pDoc.SaveAs(outputFile);
                pDoc.Close();
                cadApp.Quit();
                Console.ReadKey();
                return -1;
            }
           
            
            Console.ReadKey();
            return 0;
        }
    }
}

